package xyz.longicorn.driver.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import xyz.longicorn.driver.config.BizException;
import xyz.longicorn.driver.dao.FileInfoMapper;
import xyz.longicorn.driver.dao.FolderInfoMapper;
import xyz.longicorn.driver.dao.ShareInfoMapper;
import xyz.longicorn.driver.dto.FileItem;
import xyz.longicorn.driver.pojo.FileInfo;
import xyz.longicorn.driver.pojo.FolderInfo;
import xyz.longicorn.driver.pojo.ShareInfo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ShareService extends ServiceImpl<ShareInfoMapper, ShareInfo> {
    @Resource
    private FileInfoMapper fileInfoMapper;
    @Resource
    private FolderService folderService;

    public ShareInfo create(ShareInfo info) {
        this.save(info);
        return info;
    }

    public ShareInfo getById(String id) {
        // 查询到分享信息
        QueryWrapper q = new QueryWrapper();
        q.eq("id", id);
        q.eq("status", 1); // 只查询状态为1 0:删除  2:违规 3：xxx
        final ShareInfo one = getOne(q);

        // 为null或者长期有效则不需要判断
        if (one != null && one.getLive() != -1) {
            // 判断有效期
            long diffTime = (new Date()).getTime() - one.getCreateTime().getTime(); // 毫秒
            if (diffTime < 0) return one; // 说明有效期远远还没有到
            // 计算当前时间和创建时间的时间差
            long betweenTime = DateUtil.between(new Date(), one.getCreateTime(), DateUnit.SECOND);
            if (betweenTime > one.getLive()) { // 大于有效期
                return null;
            }
        }
        return one;
    }

    /**
     * 根据分享码、路径、密码查询对应的文件列表
     * @param id
     * @param parent
     * @param password
     * @return
     */
    public List<FileItem> listFiles(String id, String parent, String password) {
        ShareInfo info = this.getById(id);
        if (null == info) {
            throw BizException.create(1001,"分享信息有误");
        }
        // 需要先验证密码
        if (info.getPassword() != null) {
            if (!info.getPassword().equalsIgnoreCase(password)) {
                throw BizException.create(1002,"提取密码不正确");
            }
        }
        // 是否是文件
        if (info.getType() == 1) {
            // 文件
            return getFileById(info.getFileId());
        }
        return getFolderFilesById(info, parent);
    }

    private List<FileItem> getFolderFilesById(ShareInfo info, String path) {
        if(StringUtils.hasLength(path)){
//            path = path.substring(1); /
        }
        if(path == null || path.equals("/")){
            path = "";
        }
        int uid = info.getUid();
        // 查询根目录信息
        FolderInfo folder = folderService.getById(info.getFileId()); //
        String rootPath = folder.getPath(); // 获取真实的逻辑路径
        // 查询目录信息
        System.out.println(rootPath + path);
        return folderService.listFolder(uid,rootPath + path);
    }

    private List<FileItem> getFileById(Long fileId) {
        final FileInfo fileInfo = fileInfoMapper.selectById(fileId);
        FileItem item = FileItem.build(fileInfo);
        List<FileItem> list = new ArrayList<>();
        list.add(item);
        return list;
    }
}
